package org;

import org.bean.Analyzer;
import org.bean.DFA;
import org.bean.SLRProduction;
import org.example.GrammarParser;
import org.example.bean.Production;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class main {
    public static void main(String[] args) throws IOException {
        System.out.println("开始读取文法");
        GrammarParser grammarParser = new GrammarParser();
        //兼容以前的版本
        List<Production> productions = GrammarParser.parseGrammar(new File("src/main/resources/grammar2.txt"));
        List<SLRProduction> productionList = grammarParser.parseGrammar_1(new File("src/main/resources/grammar2.txt"));
        System.out.println("读取到的文法如下");
        System.out.println(productionList);

//        System.out.println("开始进行状态转换，求所有项目集");
        DFA dfa = new DFA(productionList);
        dfa.start="A";//A是拓广文法
        dfa.init();
        System.out.println("开始状态如下：");
        dfa.item0 =dfa.closure(dfa.st);
        System.out.println(dfa.item0);
        System.out.println("所有产生式如下");
        dfa.itemList=dfa.toList(dfa.item0);
        System.out.println(dfa.itemList);


        Set<List<String>> right=new HashSet<>();
        List<String> tmp=new ArrayList<>(dfa.st.slrRight);
        tmp.remove(0);
        right.add(tmp);
        productions.add(0,new Production(dfa.start,right));//增加拓广文法
        grammarParser.setProductions(productions);

        System.out.println("first集:");
        grammarParser.getAllFirst_1(productions);
        System.out.println("follow集:");
        grammarParser.getAllFollow(productions);
        dfa.setProductionList(productions);
//        dfa.getAllItem(dfa.st);//求得所有的项目只在一个集合中，不是很需要

//        dfa.get_All_Item(dfa.st);//求得所有的项目在各自的项目集，但是求得的项目集之间跳转是错误的，所以求得分析表自然也有误

        System.out.println("开始进行状态转换，求所有项目集");

        try {

            dfa.ITEMS = dfa.getAllItemAndAction(dfa.st);
        }
        catch (RuntimeException e){
            System.out.println(e.toString());
            return;
        }
        dfa.printParseTable();
        System.out.println("开始读取句子");
        //从文件中读取
        FileReader reader=new FileReader("src/main/resources/programText2.txt");
        BufferedReader bufferedReader = new BufferedReader(reader);
        String programText="";
        StringBuilder stringBuilder=new StringBuilder();
        while ((programText=bufferedReader.readLine()) != null){
            stringBuilder.append(programText);
        };
        programText=stringBuilder.toString();
        System.out.println("待分析的句子：");
        System.out.println(programText);
        if(!programText.endsWith("$"))programText=programText+"$";
        Analyzer analyzer = new Analyzer(programText,dfa.actionTableList, dfa.ITEMS, dfa.slrList);
        try {
            if (analyzer.analyzeWithLex()) {
                System.out.println("该语句符合该文法");
            } else System.out.println("这个语句不符合文法");
            ;
        }
        catch (Exception e){
            System.out.println();
            System.out.println(e.getMessage());
        }
    }
}
